package cn.zhp.controller;


import cn.zhp.entity.AppVersion;
import cn.zhp.entity.Result;
import cn.zhp.service.AppVersionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

//http://localhost:8080/guguanjia/manager/app/index
//必须起应用名，否则会导致manager请求跑到tomcat自带的manager项目上
@RestController
@RequestMapping("manager/app")
public class AppVersionController {

    @Autowired
    AppVersionService service;


    /**
     * 跳转到index页面
     * 默认走ajax响应，如果需要走视图解析器需要将返回值设置为ModelAndView
     */
    @RequestMapping("index")
    public ModelAndView index() {
        return new ModelAndView("/app/index");
    }

    /**
     * 跳转到update页
     */
    @RequestMapping("toUpdate")
    public ModelAndView toUpdate() {
        return new ModelAndView("/app/update");
    }

    /**
     * 返回列表
     */
    @RequestMapping("findAll")
    public Result findAll() {
        return new Result(true, null, service.selectAll());
    }


    /**
     * 分页查询
     */
    @RequestMapping("selectPage/{pageNum}/{size}")
    public Result selectPage(@PathVariable("pageNum") int pageNum, @PathVariable("size") int size) {
        PageInfo pageInfo = service.selectPage(pageNum, size);
        return new Result(true, null, pageInfo);
    }

    /**
     *执行修改app
     * */
    @RequestMapping("doUpdate")
    public Result doUpdate(@RequestBody AppVersion app) {
        app.setUpdateDate(new Date());
        service.updateByPrimaryKeySelective(app);
        return new Result(true, "更新成功", null);
    }

    /**
     * 插入数据
     * */
    @RequestMapping("insert")
    public Result insert(@RequestBody AppVersion app) {
        app.setCreateDate(new Date());
        app.setUpdateDate(new Date());
        app.setDelFlag("0");
        service.insertSelective(app);
        return new Result(true, "新增成功", null);
    }

    /**
     * 删除数据：不是真的删除，标记为删除
     */
    @RequestMapping("delete/{id}")
    public Result delete(@PathVariable("id") long id) {
        AppVersion appVersion = new AppVersion();
        appVersion.setId(id);
        appVersion.setUpdateDate(new Date());
        appVersion.setDelFlag("1");
        service.updateByPrimaryKeySelective(appVersion);
        return new Result(true, "删除成功", null);
    }
}
